<?php


namespace app\api\controller;

use think\Db;
use think\Exception;

/**
 * Class 申诉
 * @package app\api\controller
 */
class Appeal extends \app\common\controller\Api
{
    protected $noNeedLogin = ['appeal_guize'];
    protected $noNeedRight = ['*'];

    /**
     * 申诉
     */
    public function add_appeal(){
        $order_id = input('order_id') ? input('order_id'):$this->error('未指定订单');
        $content = input('content') ? input('content') :$this->error('请补全信息');
        $image = input('image') ? input('image'):$this->error('请补全凭证');
        $order = db('order')->find($order_id);
        $data = [];
        if ($order['status'] == 1 || $order['status'] == 2){
            Db::startTrans();
            try {
                $data['order_id'] = $order['id'];
                $user = $this->auth->getUser();
                if ($order['buy_user_id'] == $user->id){
                    //我是买家
                    $data['type'] = 1;
                    db('order')->where('id',$order_id)->update(['updatetime'=>time(),'status'=>4]);
                }else{
                    //我是卖家
                    $data['type'] = 1;
                    db('order')->where('id',$order_id)->update(['updatetime'=>time(),'status'=>5]);
                }
                $data['user_id'] = $user->id;
                $data['content'] = $content;
                $data['images'] = trim($image,',');
                $data['status'] = 1;
                $data['updatetime'] = time();
                db('appeal')->insert($data);
                Db::commit();
            }catch (Exception $exception){
                Db::rollback();
                $this->error($exception->getMessage());
            }
        }else{
            $this->error('订单暂不可申诉');
        }
        $this->success('申诉成功,请等待管理员审核');
    }

    /**
     * 申诉规则
     */
    public function appeal_guize(){
        $data = db('config')->where('name','appeal_guize')->value('value');
        $url = $this->request->domain();
        $data = str_replace('src="','style="width:100%" src="'.$url,$data);
        $this->success('ok',$data);
    }
}